/**
 *===================================
 * 封装 总环境接入
 * 2017年6月16日10:07:18 James
 * OSP移动接口封装
 * 此js不依赖任何其他第三方js
 * api 请参考 www.yt.pansoft.com/james
 * ===================================
 **/

var OSP = {
	//拨打电话
	callPhone: function(phonenumber) {
		if(isMobile.Android()) {
			OSPAndroid.callPhone(phonenumber);
		} else if(isMobile.iOS()) {
			var pathParam = {
				tel: phonenumber
			};
			OSPiOS.loadNativeDailPhone(pathParam);
		} else {

		}
	},
	//打开商品详情(参数为商品编号)
	goodDetail: function(goodBh) {
		if(isMobile.Android()) {
			OSPAndroid.goodDetail(goodBh);
		} else if(isMobile.iOS()) {
			window.webkit.messageHandlers.goGoodDetail.postMessage(goodBh)
		} else {

		}
	},
	//获取登录信息
	getUserInfo: function(callback) {
		if(isMobile.Android()) {
			callback(OSPAndroid.getUserInfo());
		} else if(isMobile.iOS()) {
			var pathParam = {
				path: ""
			};
			OSPiOS.loadNativeUserInfo(pathParam, callback);
		} else {
			callback("9999;cwpass88;2540;1234569999");
		}
	},
	//http请求
	httpSend: function(url, type, callback) {
		if(isMobile.Android()) {
			callback(OSPAndroid.httpSend(url, type));
		} else if(isMobile.iOS()) {
			var pathParam = {
				url: url,
				method: type
			};
			OSPiOS.loadNativeHttpSend(pathParam, callback);
		} else {
			$.ajax({
				type: type,
				url: url,
				timeout: 600000,
				dataType: "json",
				success: function(data) {
					callback(JSON.stringify(data));
				}
			});
		}
	},
	//打开全屏页面
	openFullScreen: function(urlstr) {
		if(isMobile.Android()) {
			OSPAndroid.openFullScreen(urlstr);
		} else if(isMobile.iOS()) {
			//var pathParam={appNum:"2411", htmlName:urlstr };
			//OSPiOS.loadNativeFullScreen(pathParam);
			var pathParam = {
				appNum: "2411",
				htmlName: urlstr
			};
			OSPiOS.loadNativeFullScreen(pathParam);
		} else {
			window.location.href = "/www/" + urlstr;
		}
	},
	//新页面打开网页(非全屏)
	openNewPage: function(urlstr) {
		if(isMobile.Android()) {
			OSPAndroid.openNewPage(urlstr);
		} else if(isMobile.iOS()) {
			window.webkit.messageHandlers.openNewPage.postMessage(urlstr)
		}
	},
	//关闭页面 注意实现关闭页面后刷新上一页面
	closePage: function(urlstr) {
		if(isMobile.Android()) {
			OSPAndroid.closePage();
		} else if(isMobile.iOS()) {
			// OSPiOS.loadNativeClose();
			var pathParam = {
				path: ""
			};
			OSPiOS.loadNativeClose();
		} else {
			window.history.go(-1);
		}
	},
	//参数浅复制
	extend: function(target, options) {
		for(name in options) {
			target[name] = options[name];
		}
		return target;
	},
	//日志输出
	log: function(message, data) {
		var log = document.getElementById('log')
		var el = document.createElement('div')
		el.className = 'logLine'
		el.innerHTML = '....... ' + message + ".......";
		log.appendChild(el);
	},
	//获取url中参数
	getPara: function(callback) {
		var paramarray = {};
		if(isMobile.Android()) {
			/**var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if(r!=null){
			   callback(unescape(r[2]));
			}**/
			var data = window.location.href.split("?")[1];
			var paramsdata = data.split("&");
			for(var i = 0; i < paramsdata.length; i++) {
				var param = paramsdata[i].split("=");
				paramarray[param[0]] = param[1];

			}
			callback(paramarray);
		} else if(isMobile.iOS()) {
			OSPiOS.loadNativeParams(function(data) {
				var paramsdata = data.split("&");
				for(var i = 0; i < paramsdata.length; i++) {
					var param = paramsdata[i].split("=");
					paramarray[param[0]] = param[1];
				}
				callback(paramarray);
			});
		} else {
			var data = window.location.href.split("?")[1];
			var paramsdata = data.split("&");
			for(var i = 0; i < paramsdata.length; i++) {
				var param = paramsdata[i].split("=");
				paramarray[param[0]] = param[1];
			}
			callback(paramarray);
		}
	},
	json2url: function(jon) {
		var newurl = "";
		for(j in jon) {
			newurl += "&" + j + "=" + jon[j];
		}
		return newurl;
	}

}

/**
 * 封装 安卓环境
 **/
var OSPAndroid= {
	//商品详情
	goodDetail: function(goodBh) {
		OSPPlugin.goodDetail(goodBh);
	},
	//新页面打开网页
	openNewPage: function(url) {
		OSPPlugin.openNewPage(url);
	},
	//拨打电话
	callPhone: function(phonenumber) {
		OSPPlugin.callPhone(phonenumber);
	},
	//获取登录信息
	getUserInfo: function() {
		return OSPPlugin.getUserInfo();
	},
	//http请求
	httpSend: function(url, type) {
		return OSPPlugin.httpSend(url, type);
	},
	//全屏打开
	openFullScreen: function(urlstr) {
		return OSPPlugin.openFullScreen(urlstr);
	},
	//关闭当前页面
	closePage: function() {
		OSPPlugin.closePage();
	}
}

/**
 * 封装 IOS环境
 **/
var bridgeiOSPub;
var OSPiOS = {
	/**
	 *  OSP环境初始化
	 **/
	initOSPBridge: function() {
        var callback=OSPiOS.initOS;
        logging("初始化开始=======");
		if(window.WebViewJavascriptBridge) {
			return callback(WebViewJavascriptBridge);
		}
		if(window.WVJBCallbacks) {
			return window.WVJBCallbacks.push(callback);
		}
		window.WVJBCallbacks = [callback];
		var WVJBIframe = document.createElement('iframe');
		WVJBIframe.style.display = 'none';
		WVJBIframe.src = 'https://__bridge_loaded__';
		document.documentElement.appendChild(WVJBIframe);
		setTimeout(function() {
			document.documentElement.removeChild(WVJBIframe)
		}, 0)
	},
	/**
	 * OSP环境初始化回调
	 */
	initOS: function(bridge) {
		logging("注册信息开始=======");
		/* Initialize your app here */

		bridge.registerHandler('invokeOSPJavaScript', function(data, responseCallback) {
			console.log("JS Echo called with:", data)
			responseCallback(data)
		});
		bridgeiOSPub = bridge;
		logging("初始化完成=======");
		/*logging("初始化完成，开始调用 原生invokeOSPNative=======");
		bridge.callHandler('invokeOSPNative', {
			'key': 'value'
		}, function responseCallback(responseData) {
			console.log("JS received response:", responseData)
			logging("调用 原生invokeOSPNative成功 返回信息====" + responseData);
		})*/
	},
	/**
	 * OSP调用json文件
	 */
	loadNativeTest: function(jsonparams, callback) {
		var defaults = {
			'key': 'value'
		};
		var jsonstr = OSP.extend(defaults, jsonparams);
		//等待环境初始化成功 每0.1秒监测一次
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	},
	/**
	 * OSP调用json文件
	 */
	loadNativeAPPJSON: function(jsonparams, callback) {
		var defaults = {
			clazz: "EFWebNativeAPPJSON"
		};
		var jsonstr = OSP.extend(defaults, jsonparams);

		//等待环境初始化成功 每0.1秒监测一次
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	},
	/**
	 * 获取网络状态
	 */
	loadNativeNetState: function(jsonparams, callback) {
		var defaults = {
			clazz: "EFWebNativeNetState"
		};
		var jsonstr = OSP.extend(defaults, jsonparams);

		//等待环境初始化成功 每0.1秒监测一次
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	},
	/**
	 * 获取登录用户的信息
	 */
	loadNativeUserInfo: function(jsonparams, callback) {
		var defaults = {
			clazz: "EFWebNativeUserInfo"
		};
		var jsonstr = OSP.extend(defaults, jsonparams);

		//等待环境初始化成功 每0.1秒监测一次
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	},
	/**
	 * 发送http请求
	 */
	loadNativeHttpSend: function(jsonparams, callback) {
		var defaults = {
			clazz: "EFWebNativeHttpRequest"
		};
		var jsonstr = OSP.extend(defaults, jsonparams);
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	},
	/**
	 * 发送http请求
	 */
	loadNativeDailPhone: function(jsonparams) {
		var defaults = {
			clazz: "EFWebNativeDailPhone"
		};
		var jsonstr = OSP.extend(defaults, jsonparams);
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	},
	/**
	 * 打开新页面
	 */
	loadNativeFullScreen: function(jsonparams) {
		var defaults = {
			clazz: "EFWebNativePushOperation"
		};
		var jsonstr = OSP.extend(defaults, jsonparams);
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	},
	/**
	 * 关闭页面并刷新页面
	 */
	loadNativeClose: function() {
		var defaults = {
			clazz: "EFWebNativeBackOperation"
		};
		var jsonstr = defaults;
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	},
	/**
	 * 获取页面参数
	 */
	loadNativeParams: function(callback) {
		var defaults = {
			clazz: "EFWebNativeParams"
		};
		var jsonstr = defaults;
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	},
	/**
	 * 商品详情
	 */
	loadNativeGoodsDetail: function(jsonparams) {
		var defaults = {
			clazz: "EFWebNativeGoodsDetail"
		};
		var jsonstr = OSP.extend(defaults, jsonparams);
		var timer = setInterval(function() { //开启定时器
			if(bridgeiOSPub) {
				bridgeiOSPub.callHandler('invokeOSPNative', JSON.stringify(jsonstr), callback);
				clearInterval(timer); //清除定时器
			}
		}, 100);
	}

	
}

//设备类型
var isMobile = {
	Android: function() {
		//return false;
		return navigator.userAgent.match(/Android/i) ? true : false;
	},
	BlackBerry: function() {
		return navigator.userAgent.match(/BlackBerry/i) ? true : false;
	},
	iOS: function() {
		//return false;
		return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
	},
	Windows: function() {
		return navigator.userAgent.match(/IEMobile/i) ? true : false;
	},
	any: function() {
		return(isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
	}
}
function initOSP(){
	if(isMobile.iOS()) {
		OSPiOS.initOSPBridge();
	}
}
initOSP();
function logging(message) {
	var log = document.getElementById('message')
	var el = document.createElement('div')
	el.innerHTML = message;
	log.appendChild(el);
}